<template>
  <div class="channel-container-101">
    <div class="title">请选择主备渠道(主渠道提交失败后走备选渠道)</div>

    <div class="box">
      <div class="select-box-scroll">
        <div
          v-for="(item, index) in tabList"
          class="item"
          :key="index"
          :info="item"
          @click="handleClickItem(item)"
          :class="{ selected: isSelected(item) }"
          :style="{ opacity: item.isCan == 1 ? '0.5' : '1' }"
        >
          <div class="left">
            <div class="radio">
              <img
                style="width: 25px; height: 25px; border-radius: 50%"
                v-if="!isSelected(item)"
                src="../../../assets/images/radio.png"
              />
              <img
                style="width: 15px; height: 15px; border-radius: 50%"
                v-if="isSelected(item) && item.selectNum == 1"
                src="../../../assets/images/zhu.png"
              />
              <img
                style="width: 15px; height: 15px; border-radius: 50%"
                v-if="isSelected(item) && item.selectNum == 2"
                src="../../../assets/images/bei.png"
              />
            </div>

            <div class="img">
              <img :src="item.icon" style="width: 35px; height: 35px; border-radius: 50%" />
            </div>

            <div class="type">
              <div class="name">{{ item.name }}</div>
              <div class="support">
                <a-tooltip placement="top">
                  <template #title>
                    {{ item.text }}
                  </template>
                  <span class="hiddenText">{{ item.text }}</span>
                </a-tooltip>
              </div>
            </div>
          </div>

          <div class="right">
            <div class="text">{{ item.desc }}:</div>
            <div class="price" v-if="item.netPrice">
              {{ '￥' + (item.netPrice / 100).toFixed(2) }}
            </div>
            <div class="price" v-else>暂不支持</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, toRefs } from 'vue'

const props = defineProps({
  tabList: Array,
  selectedChannelList: Array,
})
const { tabList, selectedChannelList } = toRefs(props)

const emit = defineEmits(['onSelectedChannel'])

const selectedChannel = ref(selectedChannelList.value)
console.log('selectedChannel=', selectedChannel.value)

// 选中渠道
const handleClickItem = item => {
  //添加不可 选中
  if (item.id == 6 || item.id == 9 || item.id == 8) {
    return
  }

  const currentIndex = selectedChannel.value.findIndex(i => i.id === item.id)

  if (currentIndex !== -1) {
    // 如果已选中，则取消选择
    selectedChannel.value.splice(currentIndex, 1)
  } else {
    // 如果没有达到最大选择数，则添加选择
    if (selectedChannel.value.length < 2) {
      // 1：主，2：备
      selectedChannel.value.push(item)

      if (selectedChannel.value.length == 1) {
        selectedChannel.value[0].selectNum = 1
      }

      if (selectedChannel.value.length == 2) {
        selectedChannel.value[0].selectNum = 1
        selectedChannel.value[1].selectNum = 2
      }
    }
  }
  console.log('selectedChannel=', selectedChannel.value)
  emit('onSelectedChannel', selectedChannel.value)
}

// 判断是否选中
const isSelected = item => {
  return selectedChannel.value.some(i => i.id === item.id)
}

watch(
  selectedChannel.value,
  (newVal, oldVal) => {
    console.log('newVal=', newVal)

    if (newVal.length > 0) {
      newVal[0].selectNum = 1
      selectedChannel.value = newVal
    }
  },
  { immediate: true },
)
</script>

<style lang="less" scoped>
.hiddenText {
  display: inline-block;
  width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  line-height: 14px;
  font-size: 10px;
}

.channel-container-101 {
  .title {
    font-size: 14px;
    color: #15181d;
    font-weight: 500;
    padding: 10px 15px;
    background-color: #fff;
  }

  .box {
    background-color: #fff;
    padding: 10px;

    .select-box-scroll {
      // height: 280px;
      overflow-y: auto;

      .item {
        padding: 4px 15px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 13px;
        color: #858a99;
        border: 1px solid #f7f8f9;
        border-radius: 3px;

        .left {
          display: flex;
          align-items: center;

          .radio {
            margin-right: 10px;
            width: 20px;
            height: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
          }

          .img {
            width: 35px;
            height: 35px;
          }

          .type {
            margin-left: 10px;
            display: flex;
            flex-direction: column;
            width: 180px;

            .name {
              color: #15181d;
              font-weight: 600;
              font-size: 12px;
            }

            .support {
              color: #858a99;
              margin-top: 3px;
            }
          }
        }

        .right {
          display: flex;
          align-items: center;

          .text {
            color: #15181d;
            font-size: 12px;
            width: 80px;
          }

          .price {
            color: #1677ff;
            font-size: 12px;
            // margin-left: 10px;
            font-weight: 600;
            width: 80px;
          }
        }
      }
    }

    .item:nth-child(n + 2) {
      margin-top: 4px;
    }

    .selected {
      border: 1px solid #1677ff;
      background-color: rgba(22, 119, 255, 0.1);
      border-radius: 3px;
    }
  }
}
</style>
